package com.btshop.goods.controller;
import com.btshop.goods.pojo.Goods;
import com.btshop.goods.pojo.Spu;
import com.btshop.goods.service.SpuService;
import com.github.pagehelper.PageInfo;
import com.util.Result;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @ClassName: SpuController
 * @description: Spu 控制层接口定义类
 * @author: 李东
 * @Date: 2020-7-20
 */
@Api(value = "SpuController")
@Slf4j
@RestController
@RequestMapping("/spu")
@CrossOrigin
public class SpuController {

    @Autowired
    private SpuService spuService;

    /**
     * 批量上架
     * @param ids
     * @return
     */
    @ApiOperation(value = "Spu批量上架",notes = "批量上架",tags = {"SpuController"})
    @PutMapping("/put/many")
    public Result putMany(@RequestBody List<Long> ids){
        spuService.putMany(ids);
        return Result.buildSuccess("批量上架成功！");
    }

    /**
     * 上架
     * @param spuId
     * @return
     */
    @ApiOperation(value = "Spu上架",notes = "上架",tags = {"SpuController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "spuId", value = "spu主键id", required = true, dataType = "Long")
    })
    @PutMapping("/put/{spuId}")
    public Result put(@PathVariable("spuId")Long spuId){
        spuService.put(spuId);
        return Result.buildSuccess("上架成功！");
    }


    /**
     * 下架
     * @param spuId
     * @return
     */
    @ApiOperation(value = "Spu下架",notes = "下架",tags = {"SpuController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "spuId", value = "spu主键id", required = true, dataType = "Long")
    })
    @PutMapping("/pull/{spuId}")
    public Result pull(@PathVariable("spuId")Long spuId){
        spuService.pull(spuId);
        return Result.buildSuccess("下架成功！");
    }


    /**
     * 审核
     * @param spuId
     * @param status 1：审核通过，2：审核不通过
     * @return
     */
    @ApiOperation(value = "Spu审核",notes = "审核",tags = {"SpuController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "spuId", value = "spu主键id", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "path", name = "status", value = "审核状态", required = true, dataType = "String")
    })
    @PutMapping("/audit/{spuId}/{status}")
    public Result audit(@PathVariable("spuId")Long spuId,@PathVariable("status")String status){
        spuService.audit(spuId,status);
        return Result.buildSuccess("操作成功！");
    }



    /**
     * 根据spuId查询商品信息
     */
    @ApiOperation(value = "Spu根据spuId查询商品信息",notes = "根据spuId查询商品信息",tags = {"SpuController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "spuId", value = "spu主键id", required = true, dataType = "Long")
    })
    @GetMapping("/goods/{spuId}")
    public Result<Goods> findGoodsBySpuId(@PathVariable("spuId") Long spuId){
        Goods goods = spuService.findGoodsBySpuId(spuId);
        return Result.buildSuccess("根据spuId查询商品信息成功！",goods);
    }


    /**
     * 商品添加
     * @param goods
     * @return
     */
    @ApiOperation(value = "Spu商品添加",notes = "商品添加",tags = {"SpuController"})
    @PostMapping("/save")
    public Result saveGoods(@RequestBody Goods goods){
        spuService.saveGoods(goods);
        return Result.buildSuccess("商品添加成功！");
    }


    /***
     * Spu分页条件搜索实现
     * @param spu
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "Spu条件分页查询",notes = "分页条件查询Spu方法详情",tags = {"SpuController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "Spu对象",value = "传入JSON数据",required = false) Spu spu, @PathVariable  int page, @PathVariable  int size){
        log.info("SpuController findPage params , spu : {} , page : {} , size : {}",spu,page,size);
        //调用SpuService实现分页条件查询Spu
        PageInfo<Spu> pageInfo = spuService.findPage(spu, page, size);
        return Result.buildSuccess("查询成功！", pageInfo);
    }

    /***
     * Spu分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "Spu分页查询",notes = "分页查询Spu方法详情",tags = {"SpuController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        log.info("SpuController findPage params , page : {} , size : {}",page,size);
        //调用SpuService实现分页查询Spu
        PageInfo<Spu> pageInfo = spuService.findPage(page, size);
        return Result.buildSuccess("查询成功！", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param spu
     * @return
     */
    @ApiOperation(value = "Spu条件查询",notes = "条件查询Spu方法详情",tags = {"SpuController"})
    @PostMapping(value = "/search" )
    public Result<List<Spu>> findList(@RequestBody(required = false) @ApiParam(name = "Spu对象",value = "传入JSON数据",required = false) Spu spu){
        log.info("SpuController findList params , spu : {} ",spu);
        //调用SpuService实现条件查询Spu
        List<Spu> list = spuService.findList(spu);
        return Result.buildSuccess("查询成功！", list);
    }

    /***
     * 根据主键删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Spu根据主键id删除",notes = "根据主键删除Spu方法详情",tags = {"SpuController"})
    @ApiImplicitParams({
                @ApiImplicitParam(paramType = "path", name ="id", value = "主键", required = true, dataType = "Long")
    })
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable("id") Long id){
        log.info("SpuController delete params ,   id : {}   ",id);
        //调用SpuService实现根据主键删除
        spuService.delete(id);
        return Result.buildSuccess("删除成功！");
    }

    /***
     * 修改Spu数据
     * @param spu
     * @param id
     * @return
     */
    @ApiOperation(value = "Spu根据id修改",notes = "根据ID修改Spu方法详情",tags = {"SpuController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name ="id", value = "主键", required = true, dataType = "Long")
     })
    @PutMapping(value="/{id}")
    public Result update(@RequestBody @ApiParam(name = "Spu对象",value = "传入JSON数据",required = false) Spu spu,@PathVariable("id") Long id){
        log.info("SpuController update params ,  spu : {},  id : {}   ",spu,id);
        //设置主键值
        spu.setId(id);
        //调用SpuService实现修改Spu
        spuService.update(spu);
        return Result.buildSuccess("修改成功！");
    }

    /***
     * 新增Spu数据
     * @param spu
     * @return
     */
    @ApiOperation(value = "Spu添加",notes = "添加Spu方法详情",tags = {"SpuController"})
    @PostMapping
    public Result add(@RequestBody  @ApiParam(name = "Spu对象",value = "传入JSON数据",required = true) Spu spu){
        log.info("SpuController add params , spu : {}",spu);
        //调用SpuService实现添加Spu
        spuService.add(spu);
        return Result.buildSuccess("添加成功！");
    }

    /***
     * 根据ID查询Spu数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Spu根据id查询",notes = "根据ID查询Spu方法详情",tags = {"SpuController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name ="id", value = "主键", required = true, dataType = "Long")
            })
    @GetMapping("/{id}")
    public Result<Spu> findById(@PathVariable("id") Long id){
        log.info("SpuController findById params ,   id : {}   ",id);
        //调用SpuService实现根据主键查询Spu
        Spu spu = spuService.findById(id);
        return Result.buildSuccess("查询成功！",spu);
    }

    /***
     * 查询Spu全部数据
     * @return
     */
    @ApiOperation(value = "查询所有Spu",notes = "查询所Spu有方法详情",tags = {"SpuController"})
    @GetMapping
    public Result<List<Spu>> findAll(){
        //调用SpuService实现查询所有Spu
        List<Spu> list = spuService.findAll();
        return Result.buildSuccess("查询成功！",list);
    }
}
